import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/8/12
 * Time: 23:31
 */
public class Solution {
    public int triangleNumber(int[] nums) {
        //1.排序
        Arrays.sort(nums);
        int count = 0;
        //2.固定最大数
        for(int i = nums.length - 1;i > 1;i--){
            int left = 0;
            int right = i - 1;
            while(left < right){
                //两数之和大于第三个数,左指针右移
                if(nums[left] + nums[right] > nums[i]){
                    count += (right - left);
                    right--;
                //两数之和小于第三个数,右指针左移
                }else{
                    left++;
                }
            }
        }
        return count;
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        int ret = s.triangleNumber(new int[]{1, 2, 3, 4, 5});
        System.out.println(ret);
    }
}